Imports Microsoft.Win32

Imports WinXound_Net.gVar



Public Class wxRegistry

    Private Declare Sub SHChangeNotify Lib "shell32.dll" _
               (ByVal wEventId As Int32, _
                ByVal uFlags As Int32, _
                ByVal dwItem1 As Int32, _
                ByVal dwItem2 As Int32)

    Private Const SHCNE_ASSOCCHANGED As Int32 = &H8000000
    Private Const SHCNF_IDLIST As Int32 = &H0&


#Region " OLD STUFFS "
    'Public Sub LoadSettings()

    '    'Check for registry keys
    '    If Registry.CurrentUser.OpenSubKey("Software\WinXound_Net") Is Nothing Then
    '        Registry.CurrentUser.CreateSubKey("Software\WinXound_Net")
    '    End If
    '    If Registry.CurrentUser.CreateSubKey("Software\WinXound_Net\Settings") Is Nothing Then
    '        Registry.CurrentUser.CreateSubKey("Software\WinXound_Net\Settings")
    '    End If
    '    If Registry.CurrentUser.CreateSubKey("Software\WinXound_Net\Colors") Is Nothing Then
    '        Registry.CurrentUser.CreateSubKey("Software\WinXound_Net\Colors")
    '    End If
    '    If Registry.CurrentUser.CreateSubKey("Software\WinXound_Net\RecentFiles") Is Nothing Then
    '        Registry.CurrentUser.CreateSubKey("Software\WinXound_Net\RecentFiles")
    '    End If


    '    Dim RegSettings As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\WinXound_Net\Settings")
    '    Dim RegColors As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\WinXound_Net\Colors")
    '    Dim RegRecentFiles As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\WinXound_Net\RecentFiles")

    '    Try
    '        ''Directory
    '        Preferences.Directory.CSoundConsole = RegSettings.GetValue("FileCsound")
    '        Preferences.Directory.Winsound = RegSettings.GetValue("FileWinsound")
    '        Preferences.Directory.CSoundAV = RegSettings.GetValue("FileCSoundAV")
    '        'Preferences.Directory.CSoundHelp = RegSettings.GetValue("FileHelp")
    '        Preferences.Directory.CSoundHelpHTML = RegSettings.GetValue("FileHelpHTML")
    '        Preferences.Directory.CSoundAVHelp = RegSettings.GetValue("FileHelpCSoundAV")
    '        Preferences.Directory.WaveEditor = RegSettings.GetValue("WaveEditor")
    '        Preferences.Directory.WorkingDir = RegSettings.GetValue("WorkingDirectory")

    '        Preferences.Directory.SFDIR = RegSettings.GetValue("SFDIR")
    '        Preferences.Directory.SSDIR = RegSettings.GetValue("SSDIR")
    '        Preferences.Directory.SADIR = RegSettings.GetValue("SADIR")
    '        Preferences.Directory.MFDIR = RegSettings.GetValue("MFDIR")
    '        Preferences.Directory.INCDIR = RegSettings.GetValue("INCDIR")


    '        ''Syntax and Text
    '        Preferences.Syntax.DefaultFontName = RegSettings.GetValue("DefaultFontName", "Courier New")
    '        Preferences.Syntax.DefaultFontSize = RegSettings.GetValue("DefaultFontSize", "10")
    '        Preferences.Syntax.DefaultTabSize = RegSettings.GetValue("TabSize", "8")
    '        Preferences.Syntax.TextForeColor = RegColors.GetValue("colorText", RGB(0, 0, 0))
    '        Preferences.Syntax.TextBackColor = RegColors.GetValue("colorBackground", RGB(255, 255, 255))
    '        Preferences.Syntax.TextSelectionForeColor = RegColors.GetValue("colorSelectionFore", RGB(0, 0, 0))
    '        Preferences.Syntax.TextSelectionBackColor = RegColors.GetValue("colorSelectionBack", RGB(192, 192, 192))

    '        Preferences.Syntax.OpcodesForeColor = RegColors.GetValue("colorOrchestra", RGB(0, 0, 255))
    '        Preferences.Syntax.OpcodesBackColor = RegColors.GetValue("colorOrchestraBK", RGB(255, 255, 255))
    '        Preferences.Syntax.TagsForeColor = RegColors.GetValue("colorCSD", RGB(220, 0, 110))
    '        Preferences.Syntax.TagsBackColor = RegColors.GetValue("colorCSDBK", RGB(255, 255, 255))
    '        Preferences.Syntax.MacrosForeColor = RegColors.GetValue("colorMacro", RGB(160, 50, 160))
    '        Preferences.Syntax.MacrosBackColor = RegColors.GetValue("colorMacroBK", RGB(255, 255, 255))
    '        Preferences.Syntax.InstrForeColor = RegColors.GetValue("colorInstrEndin", RGB(0, 0, 255))
    '        Preferences.Syntax.InstrBackColor = RegColors.GetValue("colorInstrEndinBK", RGB(255, 255, 255))
    '        Preferences.Syntax.StringsForeColor = RegColors.GetValue("colorFiles", RGB(150, 0, 0))
    '        Preferences.Syntax.StringsBackColor = RegColors.GetValue("colorFilesBK", RGB(255, 255, 255))
    '        Preferences.Syntax.RemindersForeColor = RegColors.GetValue("colorReminders", RGB(0, 150, 0))
    '        Preferences.Syntax.RemindersBackColor = RegColors.GetValue("colorRemindersBK", RGB(255, 255, 255))

    '        'Last Files loaded
    '        LastFiles(0) = RegRecentFiles.GetValue("File1", "...")
    '        LastFiles(1) = RegRecentFiles.GetValue("File2", "...")
    '        LastFiles(2) = RegRecentFiles.GetValue("File3", "...")
    '        LastFiles(3) = RegRecentFiles.GetValue("File4", "...")

    '        'General
    '        Preferences.General.ShowUtilitiesMessage = RegSettings.GetValue("ShowUtilitiesMessage", 0)


    '    Catch ex As Exception
    '        MsgBox(ex.Message, MsgBoxStyle.Information, "Error")
    '    Finally
    '        RegSettings.Close()
    '        RegColors.Close()
    '        RegRecentFiles.Close()
    '    End Try


    '    Try
    '        System.Environment.SetEnvironmentVariable("SFDIR", Preferences.Directory.SFDIR)
    '        System.Environment.SetEnvironmentVariable("SSDIR", Preferences.Directory.SSDIR)
    '        System.Environment.SetEnvironmentVariable("SADIR", Preferences.Directory.SADIR)
    '        System.Environment.SetEnvironmentVariable("MFDIR", Preferences.Directory.MFDIR)
    '        System.Environment.SetEnvironmentVariable("INCDIR", Preferences.Directory.INCDIR)
    '    Catch ex As Exception
    '        MsgBox(ex.Message, MsgBoxStyle.Information, "Error")
    '    End Try


    'End Sub

    'Public Sub SaveSettings()

    '    Dim RegSettings As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\WinXound_Net\Settings", True)
    '    Dim RegColors As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\WinXound_Net\Colors", True)

    '    Try
    '        ''Directory
    '        RegSettings.SetValue("FileCsound", Preferences.Directory.CSoundConsole)
    '        RegSettings.SetValue("FileWinsound", Preferences.Directory.Winsound)
    '        RegSettings.SetValue("FileCSoundAV", Preferences.Directory.CSoundAV)
    '        'RegSettings.SetValue("FileHelp", Preferences.Directory.CSoundHelp)
    '        RegSettings.SetValue("FileHelpHTML", Preferences.Directory.CSoundHelpHTML)
    '        RegSettings.SetValue("FileHelpCSoundAV", Preferences.Directory.CSoundAVHelp)
    '        RegSettings.SetValue("WaveEditor", Preferences.Directory.WaveEditor)
    '        RegSettings.SetValue("WorkingDirectory", Preferences.Directory.WorkingDir)

    '        RegSettings.SetValue("SFDIR", Preferences.Directory.SFDIR)
    '        RegSettings.SetValue("SSDIR", Preferences.Directory.SSDIR)
    '        RegSettings.SetValue("SADIR", Preferences.Directory.SADIR)
    '        RegSettings.SetValue("MFDIR", Preferences.Directory.MFDIR)
    '        RegSettings.SetValue("INCDIR", Preferences.Directory.INCDIR)


    '        ''Syntax and Text
    '        RegSettings.SetValue("DefaultFontName", Preferences.Syntax.DefaultFontName)
    '        RegSettings.SetValue("DefaultFontSize", Preferences.Syntax.DefaultFontSize)
    '        RegSettings.SetValue("TabSize", Preferences.Syntax.DefaultTabSize)

    '        RegColors.SetValue("colorText", Preferences.Syntax.TextForeColor)
    '        RegColors.SetValue("colorBackground", Preferences.Syntax.TextBackColor)
    '        RegColors.SetValue("colorSelectionFore", Preferences.Syntax.TextSelectionForeColor)
    '        RegColors.SetValue("colorSelectionBack", Preferences.Syntax.TextSelectionBackColor)

    '        RegColors.SetValue("colorOrchestra", Preferences.Syntax.OpcodesForeColor)
    '        RegColors.SetValue("colorOrchestraBK", Preferences.Syntax.OpcodesBackColor)
    '        RegColors.SetValue("colorCSD", Preferences.Syntax.TagsForeColor)
    '        RegColors.SetValue("colorCSDBK", Preferences.Syntax.TagsBackColor)
    '        RegColors.SetValue("colorMacro", Preferences.Syntax.MacrosForeColor)
    '        RegColors.SetValue("colorMacroBK", Preferences.Syntax.MacrosBackColor)
    '        RegColors.SetValue("colorInstrEndin", Preferences.Syntax.InstrForeColor)
    '        RegColors.SetValue("colorInstrEndinBK", Preferences.Syntax.InstrBackColor)
    '        RegColors.SetValue("colorFiles", Preferences.Syntax.StringsForeColor)
    '        RegColors.SetValue("colorFilesBK", Preferences.Syntax.StringsBackColor)
    '        RegColors.SetValue("colorReminders", Preferences.Syntax.RemindersForeColor)
    '        RegColors.SetValue("colorRemindersBK", Preferences.Syntax.RemindersBackColor)

    '        'General
    '        RegSettings.SetValue("ShowUtilitiesMessage", Preferences.General.ShowUtilitiesMessage)


    '    Catch ex As Exception
    '        MsgBox(ex.Message, MsgBoxStyle.Information, "Error")
    '    Finally
    '        RegSettings.Close()
    '        RegColors.Close()
    '    End Try

    '    Try
    '        System.Environment.SetEnvironmentVariable("SFDIR", Preferences.Directory.SFDIR)
    '        System.Environment.SetEnvironmentVariable("SSDIR", Preferences.Directory.SSDIR)
    '        System.Environment.SetEnvironmentVariable("SADIR", Preferences.Directory.SADIR)
    '        System.Environment.SetEnvironmentVariable("MFDIR", Preferences.Directory.MFDIR)
    '        System.Environment.SetEnvironmentVariable("INCDIR", Preferences.Directory.INCDIR)
    '    Catch ex As Exception
    '        MsgBox(ex.Message, MsgBoxStyle.Information, "Error")
    '    End Try


    'End Sub


    'Public Sub SaveRecentFiles()
    '    Dim RegRecentFiles As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\WinXound_Net\RecentFiles", True)

    '    Try
    '        'Last Files loaded
    '        RegRecentFiles.SetValue("File1", LastFiles(0))
    '        RegRecentFiles.SetValue("File2", LastFiles(1))
    '        RegRecentFiles.SetValue("File3", LastFiles(2))
    '        RegRecentFiles.SetValue("File4", LastFiles(3))
    '    Catch ex As Exception
    '        MsgBox(ex.Message, MsgBoxStyle.Information, "Error")
    '    Finally
    '        RegRecentFiles.Close()
    '    End Try

    'End Sub
#End Region
 

    Public Sub RegisterExtension(ByVal AppTitle As String, _
                                 ByVal FileExtension As String, _
                                 ByVal FileType As String, _
                                 ByVal IconFileName As String, _
                                 ByVal OpenWith As String, _
                                 Optional ByVal Parameters As String = "")




        If AppTitle = "" Then Exit Sub
        Dim sKeyName As String   ' Holds Key Name in registry.
        Dim sKeyValue As String  ' Holds Key Value in registry.
        'Dim ret As Int32           ' Holds error status if any from API calls.
        'Dim lphKey As Int32        ' Holds  key handle from RegCreateKey.
        Dim path As String
        Dim RK As RegistryKey

        'path = App.path
        path = Application.StartupPath
        If Right(path, 1) <> "\" Then
            path = path & "\"
        End If

        ' This creates a Root entry that called as the string of AppTitle.
        'sKeyName = AppTitle
        'sKeyValue = FileType
        'ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
        'ret& = RegSetValue&(lphKey&, "", REG_SZ, sKeyValue, 0&)

        RK = Registry.ClassesRoot.CreateSubKey(AppTitle)
        RK.SetValue("", FileType)


        ' This creates a Root entry called as the string of FileExtension associated with AppTitle.
        'sKeyName = FileExtension
        'sKeyValue = AppTitle
        'ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
        'ret& = RegSetValue&(lphKey&, "", REG_SZ, sKeyValue, 0&)

        RK = Registry.ClassesRoot.CreateSubKey(FileExtension)
        RK.SetValue("", AppTitle)
        RK.Close()


        ' This sets the command line for AppTitle.
        'Aggiungere alla funzione il nome dell'eseguibile (per winxound e winxouncsd)

        sKeyName = AppTitle
        If Parameters <> "" Then
            sKeyValue = path & OpenWith & " " & Trim(Parameters)
        Else
            ''sKeyValue = path & OpenWith & " %1"
            sKeyValue = path & OpenWith & " " & Chr(34) & "%1" & Chr(34)
        End If
        'ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
        'ret& = RegSetValue&(lphKey&, "shell\open\command", REG_SZ, _
        '                    sKeyValue, MAX_PATH)

        RK = Registry.ClassesRoot.OpenSubKey(AppTitle, True)
        RK.CreateSubKey("shell").CreateSubKey("open").CreateSubKey("command").SetValue("", sKeyValue)
        'RK.SetValue("shell\open\command", sKeyValue)


        ' This sets the icon for the file extension
        If IconFileName <> "" Then
            sKeyName = AppTitle
            sKeyValue = IconFileName
            'ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
            'ret& = RegSetValue&(lphKey&, "DefaultIcon", REG_SZ, sKeyValue, MAX_PATH)
            'RK = Registry.ClassesRoot.OpenSubKey(AppTitle, True)
            RK.CreateSubKey("DefaultIcon").SetValue("", sKeyValue)
        End If

        RK.Close()

        ' This notifies the shell that the icon has changed
        SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0)

    End Sub


    Public Sub UnRegisterExtension(ByVal AppTitle As String, ByVal FileExtension As String)

        'Delete all keys
        Registry.ClassesRoot.DeleteSubKey(FileExtension)
        Registry.ClassesRoot.DeleteSubKey(AppTitle & "\DefaultIcon")
        Registry.ClassesRoot.DeleteSubKey(AppTitle & "\Shell\Open\Command")
        Registry.ClassesRoot.DeleteSubKey(AppTitle & "\Shell\Open")
        Registry.ClassesRoot.DeleteSubKey(AppTitle & "\Shell")
        Registry.ClassesRoot.DeleteSubKey(AppTitle)

        'Notify shell on the delete and refresh the icons
        SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0)

    End Sub

End Class
